package com.zkingsoft.wechat;

import java.math.BigDecimal;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizBusiness;
import com.zkingsoft.bean.BizOrder;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.bean.DataDictionary;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.dao.BizBusinessDao;
import com.zkingsoft.dao.DataDictionaryDao;
import com.zkingsoft.externalInterface.protocol.paramProtocol.BrandWCPayRequestData;
import com.zkingsoft.externalInterface.weixinUtil.WeixinServiceUtil;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizBusinessService;
import com.zkingsoft.service.BizOrderService;
import com.zkingsoft.tools.WxUtils;

/**
 * @description 会员相关接口
 * @author 姜友瑶
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "wxapi/vip")
public class WxVipAction extends BaseAction {

	@Autowired
	private BizOrderService bizOrderService;
	@Autowired
	private BizBusinessService businessService;
	@Autowired
	private BizBusinessDao businessDao;
	@Autowired
	private WeixinServiceUtil weixinServiceUtil;
	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;
	@Autowired
	DataDictionaryDao dataDictionaryDao;
	
	/**
	 * 购买vip创建预付订单
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 */
	@RequestMapping(value = "/buyVip/{typeId}")
	public @ResponseBody AjaxResult buyVip(@PathVariable String typeId) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		DataDictionary vipType= dataDictionaryDao.selectById(typeId);
		if(vipType!=null){
			
			BizOrder bizOrder=new BizOrder();
			bizOrder.setCreateBy(loginUser.getUserId());
			bizOrder.setUpdateBy(loginUser.getUserId());
			bizOrder.setUserId(loginUser.getOpenId());
			bizOrder.setProductTitle("VIP会员时长："+vipType.getValue());// 活动标题
			bizOrder.setOrderType(AppConstance.ORDER_TYPE_VIP);
			bizOrder.setOrderStatus(AppConstance.ORDER_STATUS_NOR);
			bizOrder.setPayStatus(AppConstance.PAY_STATUS_DZF);// 待支付
			bizOrder.setPayMoney(new BigDecimal(vipType.getExtValue()));
			bizOrder.setProductPrice(new BigDecimal(vipType.getExtValue()));
			bizOrder.setOrderId(UUIDUtil.getRandomID());
			bizOrder.setOrderNo(WxUtils.getOrderNum());// 生成订单号
			bizOrder.setOrderRemark(typeId);
			try {
				BrandWCPayRequestData payData = weixinServiceUtil.createOrder("购买VIP", bizOrder.getOrderNo(),
						new BigDecimal(vipType.getExtValue()).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(), loginUser.getOpenId(), bizOrder.getOrderId());
				if (payData != null) {
					bizOrderService.add(bizOrder);
					LogUtil.debug("创建vip预支付订单成功！#订单编号：{}", bizOrder.getOrderNo());
					AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Arrays.asList(payData));
					result.putInMap("orderId", bizOrder.getOrderId());
					return result;
				}else{
					LogUtil.error("创建购买VIP预支付订单异常:预支付返回参数为空");
					throw new GlobleException("创建订单失败，请稍后再试");
				}
			} catch (Exception e) {
				LogUtil.error("创建购买VIP预支付订单异常:{}", e, bizOrder);
				throw new GlobleException("创建订单失败，请稍后再试");
			}
		}else{
			return new AjaxResult(AjaxResult.STATUS_FAIL,"购买的商品不存在");
		}
		
	}

	/**
	 * 更新商家
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年9月6日
	 * @param bizBusiness
	 * @return
	 */
	@RequestMapping(value = "/modify")
	public @ResponseBody AjaxResult modify(@RequestBody BizBusiness bizBusiness) {
		bizBusiness.setOpenId(redisUserLoginUtils.getLoginUser().getOpenId());
		int i = businessService.modifyByModel(bizBusiness);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "商户");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
		}
	}

	/**
	 * 查询商户没有分页
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年9月6日
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "/findBusiness")
	public @ResponseBody AjaxResult findByOpenId(@RequestBody BizBusiness bizBusiness) {
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, businessService.findByModel(bizBusiness));
		return result;
	}

}